﻿using StartEAS_API.Utils;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.DAL.ProfessionalD
{
    public class ProfessionalDAL
    {
        private readonly StarsEASContext _context;
        public ProfessionalDAL(StarsEASContext context)
        {
            _context = context;
        }

        public int AddProfessional(string token, string professionalName, int CollegeId) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.FirstOrDefault(u => u.Token == token);
            if (user == null) {
                AppLog.WriteError($"新增专业信息---账户{token}尝试登录但登录失败", true);
                return -1;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"新增专业信息---账户{token}尝试登录但非教务处账户", true);
                return -1;
            }
            if (professionalName == null || CollegeId == 0)
            {
                AppLog.WriteError($"新增专业信息---账户{user.Username}尝试新增但专业名称为空", true);
                return -1;
            }
            Professional professional = new Professional();
            _context.Professionals.Add(new Professional() { ProfessionalId=0,ProfessionalName=professionalName, CollegeId= CollegeId });
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"新增专业信息---添加专业信息{professionalName}失败", true);
                return 0;
            }
            AppLog.WriteInfo($"新增专业信息---添加专业信息{professionalName}成功", true);
            return result;
        }

        public int UpdateProfessional(string token, Professional data) {
            StarsEASContext _context = new StarsEASContext();
            User user = _context.Users.FirstOrDefault(u => u.Token == token);
            if (user == null)
            {
                AppLog.WriteError($"修改专业信息---账户{token}尝试登录但登录失败", true);
                return -1;
            }
            if (user.PositionId != 6)
            {
                AppLog.WriteError($"修改专业信息---账户{token}尝试登录但非教务处账户", true);
                return -1;
            }
            if (data == null)
            {
                AppLog.WriteError($"修改专业信息---账户{user.Username}尝试新增但专业信息为空", true);
                return -1;
            }
            Professional professional = _context.Professionals.Find(data.ProfessionalId);
            if (professional == null)
            {
                AppLog.WriteError($"修改专业信息---账户{user.Username}尝试修改但{data.ProfessionalId}未查询到专业信息", true);
                return -1;
            }
            professional.ProfessionalName = data.ProfessionalName;
            professional.CollegeId = data.CollegeId;
            int result = _context.SaveChanges();
            if (result == 0)
            {
                AppLog.WriteError($"修改专业信息---修改专业信息{data.ProfessionalName}失败", true);
                return 0;
            }
            AppLog.WriteInfo($"修改专业信息---修改专业信息{data.ProfessionalName}成功", true);
            return result;
        }

        public IEnumerable<Professional> GetAllProfessionals() {
            StarsEASContext _context = new StarsEASContext();
            IEnumerable<Professional> professionals = _context.Professionals.ToList();
            AppLog.WriteInfo($"查询专业信息---查询到{professionals.Count()}条专业信息", true);
            return professionals;
        }

        public Professional GetProfessionalById(int professionalId)
        {
            StarsEASContext _context = new StarsEASContext();
            Professional professional = _context.Professionals.Find(professionalId);
            AppLog.WriteInfo($"查询专业信息---查询{professionalId}的专业信息", true);
            return professional;
        }

        public IEnumerable<Professional> GetProfessionalsByCollegeId(int CollegeId) {
            StarsEASContext _context = new StarsEASContext();
            IEnumerable<Professional> professionals = _context.Professionals.Where(p => p.CollegeId == CollegeId).ToList();
            if (professionals == null) {
                AppLog.WriteError($"查询专业信息---{CollegeId}未查询到专业列表", true);
                return null;
            }
            AppLog.WriteInfo($"查询专业信息---{CollegeId}查询到{professionals.Count()}条专业信息", true);
            return professionals;
        }


    }
}
